package com.linkage.controller.order;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.linkage.pojo.commodity.Book;
import com.linkage.pojo.consumer.ContactDetail;
import com.linkage.pojo.consumer.User;
import com.linkage.pojo.order.SingleOrder;
import com.linkage.service.background.BookManageService;
import com.linkage.service.commodity.BookService;
import com.linkage.service.consumer.EditContactDetailsService;
import com.linkage.service.consumer.UserService;
import com.linkage.service.order.SingleOrderService;

@Controller
@RequestMapping("/order")
public class orderController {

	@Autowired
	private SingleOrderService singleOrderService;
    
    @Autowired
    private BookService bookService;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private EditContactDetailsService editContactDetailsService;
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String list() {
	    System.out.println("8888");
		
	    return "order/placeOrder";
	}
	
	@Autowired
	BookManageService bookManagerService;
	
	@RequestMapping(value="/{userId}/{bookId}", method=RequestMethod.POST)
	public String placeOrder(@PathVariable("userId") Integer userId,@PathVariable("bookId") Integer bookId,Integer quantity){
	    
	     Book book=this.bookService.listSelectedBook(bookId);
	     
	     int bookStockNumber=book.getStockNumber();
	     if(quantity>bookStockNumber){
	         return "order/placeOrder";
	     }
	     
	     User user=this.userService.getOneUserById(userId);
	     if(user.getAccount() < book.getBookPrice()*quantity){
	         return "order/placeOrder";
	     }
	     
	     ContactDetail contactDetail=this.editContactDetailsService.selectDefaultContactDetail(userId);
	     SingleOrder singleOrder=new SingleOrder();
         
	     singleOrder.setBookId(book.getBookId());
         singleOrder.setUserId(user.getUserId());
         singleOrder.setState(1);
         Date date=new Date();
         singleOrder.setOrderTime(date);
         singleOrder.setQuantity(quantity);
         singleOrder.setTotalPrice(book.getBookPrice()*quantity);
         singleOrder.setPhoneNumber(contactDetail.getPhoneNumber());
         singleOrder.setCity(contactDetail.getCity());
         singleOrder.setProvince(contactDetail.getProvince());
         singleOrder.setRoad(contactDetail.getRoad());
         singleOrder.setSuburb(contactDetail.getSuburb());
         singleOrder.setReferenceNumber(this.singleOrderService.generatePreference(userId, book.getBookId()));
         
         this.singleOrderService.saveSingleOrder(singleOrder);
         
         book.setStockNumber(book.getStockNumber()-quantity);
         boolean b=this.bookManagerService.editBook(book);
         
         user.setAccount(user.getAccount()-book.getBookPrice()*quantity);
         this.userService.editProfile(user);
         
         
         return "order/index";
	    
	}

}
